﻿"use strict";

$(document).ready(function () {
    var ConfigSettings = {
        element: '',
        url: '',
        init: function (element) {
            ConfigSettings.element = element;
            ConfigSettings.url = _spPageContextInfo.webAbsoluteUrl +
                "/_api/web/lists/getByTitle('Settings')/items?select=Title,FullScrollableDisplayThumbnails,FullScrollableCAMLQuery&$filter=FullScrollableInstance eq 'Home'";
        },
        load: function () {
            $.ajax(
                {
                    url: ConfigSettings.url,
                    method: "GET",
                    headers: {
                        "accept": "application/json;odata=verbose",
                    },
                    success: ConfigSettings.onSuccess,
                    error: ConfigSettings.onError
                }
              );


        },
        onSuccess: function (data) {
            var results = data.d.results;

            var html = "<table class='DataResults'>";

            if (results.length === 0) {
                html += "<tr><td colspan =3>No 'Home'settings entries made yet, you need to do that now</td></tr>";
            }
            html += "<tr>";
            html += "<th>";
            html += "Title";
            html += "</th>";
            html += "<th>";
            html += "Thumbnails";
            html += "</th>";
            html += "<th>";
            html += "CAML Query";
            html += "</th>";
            html += "</tr>";
            for (var i = 0; i < results.length; i++) {
                html += "<tr>";
                html += "<td>";
                html += results[i].Title;
                html += "</td>"
                html += "<td>";
                html += results[i].FullScrollableDisplayThumbnails;
                html += "</td>"
                html += "<td>";
                html += results[i].FullScrollableCAMLQuery;
                html += "</td>"
                html += "</tr>";
            }
            html += "</table>";
            ConfigSettings.element.html(html);
        },
        onError: function (err) {
            var html = JSON.stringify(err);
            ConfigSettings.element.html(html);
        }
    }
    ConfigSettings.init($('#settingsDiv'));
    ConfigSettings.load();

});

